import axios from "axios";
import { getHashUrl } from './getHashUrl'
import {getQueryVariable} from './getQueryParam'

const http = axios.create({
  baseURL: '/dii',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  },
})


http.interceptors.request.use(
  (config) => {
    // const token = uni.getStorageSync('USER_TOKEN')
    // config.headers.Authorization = `Bearer token = " 60e218972ec8c52d8c4e7091114ff705 "`
    /*  if (config.custom.auth) {
       // config.header.Authorization = `Bearer token = " ${token} "`
       config.header.Authorization = `Bearer token = " 60e218972ec8c52d8c4e7091114ff705 "`
     } */
    Object.keys(config.params || {}) ||
      [].forEach(k => {
        if (config.params[k] === undefined) config.params[k] = null
      })
    const queryParams = getQueryVariable()
    const token = queryParams.token
    if (!token) alert(`${location.href}`)
    config.headers.Authorization = `Bearer token = "${token}"`
    // config.url = getHashUrl(config, token)
    config.params = {}
    return config
  },
  config => {
    // 可使用async await 做异步操作
    return Promise.reject(config)
  }
)

http.interceptors.response.use((response) => {
  if (response.data.code !== 200) {
    throw new Error(res.data.msg)
  }
  return response.data
})


export default http